home *** CD-ROM | disk | FTP | other *** search
/ PC World 2007 June / PCWorld_2007-06_cd.bin / v cisle / tclock / tclocklight-040702-3.exe / source / player / main.c < prev    next >
C/C++ Source or Header  |  2004-05-10  |  9KB  |  367 lines

  1. /*-------------------------------------------------------------
  2.   main.c : TClock Player
  3.   (C) Kazuto Sato 1997-2003
  4.   For the license, please read readme.txt.
  5.   
  6.   Written by Kazubon, Nanashi-san
  7. ---------------------------------------------------------------*/
  8.  
  9. #include "tcplayer.h"
  10.  
  11. /* Globals */
  12.  
  13. BOOL ExecCommandString(HWND hwnd, const char *command);
  14.  
  15. HINSTANCE g_hInst;                 // instance handle
  16. char      g_mydir[MAX_PATH];       // path to this exe file
  17. BOOL      g_bIniSetting = FALSE;   // save setting to ini file?
  18. char      g_inifile[MAX_PATH];     // ini file name
  19. char      g_langfile[MAX_PATH];    // tclang.txt
  20. HFONT     g_hfontDialog = NULL;    // dialog font
  21. HWND      g_hwndClock = NULL;      // clock window handle
  22. HWND      g_hwndPlayer = NULL;     // main window
  23.  
  24. /* Statics */
  25.  
  26. static int TCPlayerMain(void);
  27. static void InitTCPlayer(void);
  28. static LRESULT CALLBACK WndProcPlayer(HWND, UINT, WPARAM, LPARAM);
  29. static void OnCreate(HWND hwnd);
  30. static void OnDestroy(HWND hwnd);
  31. static void OnCopyData(HWND hwnd, HWND hwndFrom, COPYDATASTRUCT* pcds);
  32. static void SetOnContextMenu(void);
  33. static void CheckCommandLine(HWND hwnd, BOOL bPrev);
  34.  
  35. /*-------------------------------------------
  36.   WinMain
  37. ---------------------------------------------*/
  38. #ifdef NODEFAULTLIB
  39. void WINAPI WinMainCRTStartup(void)
  40. {
  41.     g_hInst = GetModuleHandle(NULL);
  42.     ExitProcess(TCPlayerMain());
  43. }
  44. #else
  45. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  46.     LPSTR lpCmdLine, int nCmdShow)
  47. {
  48.     g_hInst = hInstance;
  49.     return TCPlayerMain();
  50. }
  51. #endif
  52.  
  53. /*-------------------------------------------
  54.   main routine
  55. ---------------------------------------------*/
  56. int TCPlayerMain(void)
  57. {
  58.     MSG msg;
  59.     WNDCLASS wndclass;
  60.     HWND hwnd;
  61.     
  62.     // not to execute the program twice
  63.     hwnd = FindWindow(CLASS_TCLOCKPLAYER, NULL);
  64.     if(hwnd != NULL)
  65.     {
  66.         CheckCommandLine(hwnd, TRUE);
  67.         return 1;
  68.     }
  69.     
  70.     InitTCPlayer();
  71.     
  72.     // register a window class
  73.     wndclass.style         = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
  74.     wndclass.lpfnWndProc   = WndProcPlayer;
  75.     wndclass.cbClsExtra    = 0;
  76.     wndclass.cbWndExtra    = 0;
  77.     wndclass.hInstance     = g_hInst;
  78.     wndclass.hIcon         = LoadIcon(g_hInst, MAKEINTRESOURCE(IDI_TCLOCK));
  79.     wndclass.hCursor       = LoadCursor(NULL, IDC_ARROW);
  80.     wndclass.hbrBackground = (HBRUSH)(COLOR_3DFACE+1);
  81.     wndclass.lpszMenuName  = NULL;
  82.     wndclass.lpszClassName = CLASS_TCLOCKPLAYER;
  83.     RegisterClass(&wndclass);
  84.     
  85.     // create a hidden window
  86.     g_hwndPlayer = CreateWindowEx(0,
  87.         CLASS_TCLOCKPLAYER, "TClock Player", WS_OVERLAPPEDWINDOW,
  88.         CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,
  89.         NULL, NULL, g_hInst, NULL);
  90.     // ShowWindow(g_hwndPlayer, SW_SHOW);
  91.     // UpdateWindow(g_hwndPlayer);
  92.     
  93.     while(GetMessage(&msg, NULL, 0, 0))
  94.     {
  95.         if(g_hDlg && IsWindow(g_hDlg) && IsDialogMessage(g_hDlg, &msg)) ;
  96.         else
  97.         {
  98.             TranslateMessage(&msg);
  99.             DispatchMessage(&msg);
  100.         }
  101.     }
  102.     
  103.     return msg.wParam;
  104. }
  105.  
  106. /*-------------------------------------------
  107.   initialize
  108. ---------------------------------------------*/
  109. void InitTCPlayer(void)
  110. {
  111.     GetModuleFileName(g_hInst, g_mydir, MAX_PATH);
  112.     del_title(g_mydir);
  113.     strcpy(g_inifile, g_mydir);
  114.     add_title(g_inifile, "tclock.ini");
  115.     g_bIniSetting = TRUE;
  116.     
  117.     // common/langcode.c
  118.     FindFileWithLangCode(g_langfile, GetUserDefaultLangID(), TCLANGTXT);
  119.     g_hfontDialog = CreateDialogFont();
  120.     
  121.     g_hwndClock = GetClockWindow();
  122.     
  123.     SetOnContextMenu();
  124. }
  125.  
  126. /*-------------------------------------------
  127.   window procedure
  128. ---------------------------------------------*/
  129. LRESULT CALLBACK WndProcPlayer(HWND hwnd, UINT message,
  130.     WPARAM wParam, LPARAM lParam)
  131. {
  132.     switch (message)
  133.     {
  134.         case WM_CREATE:
  135.             OnCreate(hwnd);
  136.             return 0;
  137.         case WM_DESTROY:
  138.             OnDestroy(hwnd);
  139.             return 0;
  140.         case WM_TIMER:
  141.             switch (wParam)
  142.             {
  143.                 case IDTIMER_PLAYER:
  144.                     OnTimerPlayer(hwnd);
  145.                     break;
  146.             }
  147.             return 0;
  148.         // show dialog box
  149.         case PLAYERM_SHOWDLG:
  150.             OnShowDialog(hwnd);
  151.             return 0;
  152.         // add item to tcmenu*.txt
  153.         case PLAYERM_REQUESTMENU:
  154.             OnRequestMenu(hwnd, FALSE);
  155.             return 0;
  156.         case PLAYERM_STOP:
  157.             StopPlayer(hwnd);
  158.             PostMessage(hwnd, WM_CLOSE, 0, 0);
  159.             return 0;
  160.         case PLAYERM_PAUSE:
  161.             PausePlayer(hwnd);
  162.             return 0;
  163.         case PLAYERM_NEXT:
  164.             PrevNextPlayer(hwnd, TRUE);
  165.             return 0;
  166.         case PLAYERM_PREV:
  167.             PrevNextPlayer(hwnd, FALSE);
  168.             return 0;
  169.         
  170.         case MM_MCINOTIFY:
  171.             OnMCINotifyPlayer(hwnd, wParam, (LONG)lParam);
  172.             return 0;
  173.         
  174.         case WM_COPYDATA:
  175.             OnCopyData(hwnd, (HWND)wParam, (COPYDATASTRUCT*)lParam);
  176.             return 0;
  177.     }
  178.     return DefWindowProc(hwnd, message, wParam, lParam);
  179. }
  180.  
  181. /*-------------------------------------------------------
  182.   WM_CREATE message
  183. ---------------------------------------------------------*/
  184. void OnCreate(HWND hwnd)
  185. {
  186.     CreateWindowEx(WS_EX_CLIENTEDGE, "listbox", "",
  187.         WS_CHILD|WS_VISIBLE|WS_VSCROLL|LBS_SORT,
  188.         0, 0, 400, 80,
  189.         hwnd, (HMENU)IDC_LIST, g_hInst, NULL);
  190.     
  191.     InitPlayer(hwnd); // player.c
  192.     
  193.     CheckCommandLine(hwnd, FALSE);
  194.     
  195.     SetTimer(hwnd, IDTIMER_PLAYER, 1000, NULL);
  196. }
  197.  
  198. /*-------------------------------------------------------
  199.   WM_DESTROY message
  200. ---------------------------------------------------------*/
  201. void OnDestroy(HWND hwnd)
  202. {
  203.     StopPlayer(hwnd);
  204.     
  205.     KillTimer(hwnd, IDTIMER_PLAYER);
  206.     
  207.     if(g_hfontDialog) DeleteObject(g_hfontDialog);
  208.     
  209.     PostQuitMessage(0);
  210. }
  211.  
  212. /*-------------------------------------------------------
  213.   WM_COPYDATA message
  214. ---------------------------------------------------------*/
  215. void OnCopyData(HWND hwnd, HWND hwndFrom, COPYDATASTRUCT* pcds)
  216. {
  217.     const char *p = (char *)pcds->lpData;
  218.     
  219.     switch(pcds->dwData)
  220.     {
  221.         case COPYDATA_PLAY:
  222.             if(Player(hwnd, p) == FALSE)  // player.c
  223.             {
  224.                 if(!g_hDlg)
  225.                     PostMessage(hwnd, WM_CLOSE, 0, 0);
  226.             }
  227.             break;
  228.     }
  229. }
  230.  
  231. /*------------------------------------------------
  232.   [OnContextMenu]
  233.   AppN=TClockPlayerClass,PLAYERM_REQUESTMENU
  234. -------------------------------------------------*/
  235. void SetOnContextMenu(void)
  236. {
  237.     char entry[20], buf[100], cname[80], num[20];
  238.     BOOL b;
  239.     int i;
  240.     
  241.     for(i = 0; ; i++)
  242.     {
  243.         wsprintf(entry, "App%d", i + 1);
  244.         GetMyRegStr("OnContextMenu", entry, buf, 100, "");
  245.         b = FALSE;
  246.         if(buf[0])
  247.         {
  248.             parse(cname, buf, 0, 80);
  249.             parse(num, buf, 1, 20);
  250.             if(strcmp(cname, CLASS_TCLOCKPLAYER) == 0)
  251.             {
  252.                 if(PLAYERM_REQUESTMENU == atoi(num)) break;
  253.                 else b = TRUE;
  254.             }
  255.         }
  256.         else b = TRUE;
  257.         
  258.         if(b)
  259.         {
  260.             wsprintf(buf, "%s,%d", CLASS_TCLOCKPLAYER, PLAYERM_REQUESTMENU);
  261.             SetMyRegStr("OnContextMenu", entry, buf);
  262.             break;
  263.         }
  264.     }
  265. }
  266.  
  267. /*-------------------------------------------
  268.    process command line option
  269. ---------------------------------------------*/
  270. void CheckCommandLine(HWND hwnd, BOOL bPrev)
  271. {
  272.     char name[20], value[20];
  273.     char *p, *sp;
  274.     int i;
  275.     BOOL bOption = FALSE;
  276.     BOOL bquot = FALSE;
  277.     
  278.     p = GetCommandLine();
  279.     
  280.     while(*p == ' ') p++;
  281.     if(*p == '\"') { bquot = TRUE; p++; }
  282.     while(*p)
  283.     {
  284.         if(bquot) { if(*p == '\"') { p++; break; } }
  285.         else if(*p == ' ') break;
  286.         p++;
  287.     }
  288.     while(*p == ' ') p++;
  289.     sp = p;
  290.     
  291.     while(*p)
  292.     {
  293.         if(*p == '/')
  294.         {
  295.             p++;
  296.             for(i = 0; *p && *p != ' ' && i < 19; i++)
  297.             {
  298.                 name[i] = *p++;
  299.             }
  300.             name[i] = 0;
  301.             while(*p == ' ') p++;
  302.             
  303.             value[0] = 0;
  304.             if(*p && *p != '/')
  305.             {
  306.                 for(i = 0; *p && i < 19; i++)
  307.                     value[i] = *p++;
  308.                 value[i] = 0;
  309.             }
  310.             
  311.             if(strcmp(name, "stop") == 0)
  312.             {
  313.                 if(bPrev) PostMessage(hwnd, PLAYERM_STOP, 0, 0);
  314.                 bOption = TRUE;
  315.             }
  316.             else if(strcmp(name, "pause") == 0)
  317.             {
  318.                 if(bPrev) PostMessage(hwnd, PLAYERM_PAUSE, 0, 0);
  319.                 bOption = TRUE;
  320.             }
  321.             else if(strcmp(name, "next") == 0)
  322.             {
  323.                 if(bPrev) PostMessage(hwnd, PLAYERM_NEXT, 0, 0);
  324.                 bOption = TRUE;
  325.             }
  326.             else if(strcmp(name, "prev") == 0)
  327.             {
  328.                 if(bPrev) PostMessage(hwnd, PLAYERM_PREV, 0, 0);
  329.                 bOption = TRUE;
  330.             }
  331.         }
  332.         else p++;
  333.     }
  334.     
  335.     if(!bOption)
  336.     {
  337.         p = sp;
  338.         
  339.         if(*p)
  340.         {
  341.             if(bPrev)
  342.                 SendStringToOther(hwnd, NULL, p, COPYDATA_PLAY);
  343.             else
  344.             {
  345.                 if(Player(hwnd, p) == FALSE)  // player.c
  346.                     PostMessage(hwnd, WM_CLOSE, 0, 0);
  347.             }
  348.         }
  349.         else
  350.             PostMessage(hwnd, TIMERM_SHOWDLG, 0, 0);
  351.     }
  352. }
  353.  
  354. /* -------------------- Utilities ---------------------------------------*/
  355.  
  356. /*-------------------------------------------
  357.   called in PlayFile function
  358. ---------------------------------------------*/
  359. BOOL ExecCommandString(HWND hwnd, const char *command)
  360. {
  361.     SendStringToOther(GetTClockMainWindow(), hwnd, command,
  362.         COPYDATA_EXEC);
  363.     
  364.     return FALSE;
  365. }
  366.  
  367.